package com.gjzhao.fmmall.controller;

import com.gjzhao.fmmall.entity.Orders;
import com.gjzhao.fmmall.service.OrderService;
import com.gjzhao.fmmall.vo.RespResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 订单添加接口
 * @Author: gjzhao
 * @Date Created in 2022-02-08 5:36 下午
 */
@RestController
@RequestMapping("/orders")
@CrossOrigin
@Api(tags = "订单管理", value = "订单处理接口")
public class OrderController extends BaseController {


    @Autowired
    private OrderService orderService;

    @PostMapping("/add")
    @ApiOperation(value = "添加订单")
    @ApiImplicitParam(dataType = "string", name = "cartIds", value = "购物车ids", required = true)
    public RespResultVO add(@RequestBody Orders order,
                            String cartIds) {

        RespResultVO resultVO = null;
        try {
            resultVO = orderService.addOrder(cartIds, order);
        } catch (Exception e) {
            logger.info("-----提交订单失败:[" + e.getMessage() + "]-----");
            resultVO = RespResultVO.failed("发生异常，提交订单失败");
        }
        return resultVO;
    }

    @GetMapping("/getOrder")
    @ApiOperation(value = "查询订单")
    @ApiImplicitParam(dataType = "string", name = "orderId", value = "订单id", required = true)
    public RespResultVO getOrder(String orderId) {

        RespResultVO resultVO = null;
        try {
            resultVO = orderService.getOrder(orderId);
        } catch (Exception e) {
            logger.info("-----查询订单失败:[" + e.getMessage() + "]-----");
            resultVO = RespResultVO.failed("发生异常，查询订单失败");
        }
        return resultVO;
    }


}
